/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.servlet;

import com.database.SQLConnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Insect
 */
public class RegisterNewUserServlet extends HttpServlet {
   
   

   
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String userName = (String) request.getParameter("username");
        boolean isUserNameAvailable = checkUserNameAvailability(userName.trim());
        if(isUserNameAvailable){
            String password = (String) request.getParameter("password");
            boolean isCommitSuccessful=createUserInDB(userName,password);
            if(isCommitSuccessful)
                getServletContext().getRequestDispatcher("/common/registrationSuccessful.jsp").forward(request, response);
            else
                getServletContext().getRequestDispatcher("/common/registrationError.jsp").forward(request, response);
        }else{
            getServletContext().getRequestDispatcher("/common/registrationError.jsp").forward(request, response);
        }


    }

    private boolean checkUserNameAvailability(String userName) {
        if(userName != null){
            Connection con = (Connection) new SQLConnection().getMySQLConnection();
            try {
                PreparedStatement pstmt = con.prepareStatement("Select count(*) as userNameCount from user where username like ?");
                pstmt.setString(1, userName);
                ResultSet rs=pstmt.executeQuery();
                int userNameCount=0;
                while(rs.next()){
                    userNameCount = rs.getInt("userNameCount");
                }
                if(userNameCount>0)
                    return false;
            } catch (SQLException ex) {
                Logger.getLogger(RegisterNewUserServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            return true;
        }
        return false;
    }

    private boolean createUserInDB(String userName, String password) {
        if (userName != null && password != null) {
            Connection con = (Connection) new SQLConnection().getMySQLConnection();
            try {
                PreparedStatement pstmt = con.prepareStatement("Insert into user values (0,?,?)");
                pstmt.setString(1, userName);
                pstmt.setString(2, password);
                int result=pstmt.executeUpdate();
                if(result==0 && result>1)
                    return false;
            } catch (SQLException ex) {
                Logger.getLogger(RegisterNewUserServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return true;
    }

}
